package com.zmn.oms.dubbo.impl.zmn.order;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.oms.business.interfaces.work.OrderWorkListBService;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.common.enums.EncryptFieldTypeEnum;
import com.zmn.oms.dubbo.dto.zmn.order.OrderListDetailDRO;
import com.zmn.oms.dubbo.dto.zmn.order.OrderListPageDRO;
import com.zmn.oms.dubbo.dto.zmn.order.factory.FcOrderWorkListServiceItemDRO;
import com.zmn.oms.model.entity.work.OrderWorkQuery;
import com.zmn.base.oms.common.model.es.orderwork.EsOrderWork;
import com.zmn.oms.model.vo.work.detail.ZsOrderWorkDetailVO;
import com.zmn.oms.services.interfaces.orderencrypt.OrderEncryptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 类描述：订单 remote service
 *
 * @author liuying
 * @date 2018/11/07 15:10
 * @see OrderWorkListBaseRemoteService
 */
@Deprecated
public abstract class OrderListBaseRemoteService {

    static final String TAG = "啄木鸟-列表订单";

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    protected OrderWorkListBService orderWorkListBService;
    @Autowired
    protected OrderEncryptService orderEncryptService;

    /**
     * 列表查询
     *
     * @param query
     * @return
     */
    public ResponseDTO<OrderListPageDRO> listOrder(OrderWorkQuery query) {
        // 查询
        List<EsOrderWork> esOrderWorks = orderWorkListBService.listPageOrderWorkByQuery(query);

        // 解密电话和地址
        orderEncryptService.batchDecryptEsOrderWork(esOrderWorks, EncryptFieldTypeEnum.USER_TELPHONE, EncryptFieldTypeEnum.USER_ADDRESS);


        // 转 vo
        List<OrderListDetailDRO> list = Lists.newArrayList();

        esOrderWorks.forEach((esOrderWork -> {
            OrderListDetailDRO vo = BeanMapper.map(esOrderWork, OrderListDetailDRO.class);
            vo.setContactName(esOrderWork.getContact().getContactName());
            vo.setContactPhone(esOrderWork.getContact().getPhone());
            vo.setUserName(esOrderWork.getContact().getUserName());
            vo.setPhone(esOrderWork.getContact().getPhone());
            vo.setAddress(esOrderWork.getContact().getAddress());
            vo.setStreet(esOrderWork.getContact().getStreet());
            vo.setProvinceName(esOrderWork.getProvinceName());
            vo.setCityName(esOrderWork.getCityName());
            vo.setCountyName(esOrderWork.getCountyName());
            vo.setProvinceId(esOrderWork.getProvinceId());
            vo.setCityId(esOrderWork.getCityId());
            vo.setCountyId(esOrderWork.getCountyId());
            vo.setWorkId(esOrderWork.getId());
            vo.setWorkType(esOrderWork.getType());
            vo.setDiscountAmount(esOrderWork.getDiscountAmount());
            vo.setChannelName(esOrderWork.getChannelName());

            if (!Objects.isNull(esOrderWork.getShopOrder())) {
                vo.setShopName(esOrderWork.getShopOrder().getShopName());
            }

            if (!Objects.isNull(esOrderWork.getFactoryOrder())) {
                vo.setRemoteAmount(Optional.ofNullable(esOrderWork.getFactoryOrder().getRemoteAmount()).orElse(0));
            }

            if (Objects.equals(GlobalConsts.PLAT_MARK_SAAS, esOrderWork.getPlatWork())) {
                convertStatus(vo, esOrderWork.getStatus(), esOrderWork.getResultStatus());
            }

            vo.setServiceItemList(BeanMapper.mapList(esOrderWork.getServiceItemList(), FcOrderWorkListServiceItemDRO.class));

            list.add(vo);
        }));

        OrderListPageDRO pageDRO = new OrderListPageDRO();
        pageDRO.setTotalCount(query.getTotalCount());
        pageDRO.setList(list);

        return ResponseUtil.responseSuccess(pageDRO, GlobalConsts.SUCCESS_MSG);
    }

    /**
     * SAAS服务商状态转换
     * @param vo
     * @param status
     * @param resultStatus
     */
    private void convertStatus(OrderListDetailDRO vo, Integer status, Integer resultStatus) {
        //1.待派单--》已分单
        List<Integer> supportStatusList = Lists.newArrayList(OrderStatusConsts.WORK_STATUS_ASSIGN);
        List<Integer> supportResultStatusList = Lists.newArrayList(OrderStatusConsts.WORK_RESULT_DOING);

        //2.服务中--》已派单、已领单、已上门
        List<Integer> supportStatusList1 = Lists.newArrayList(OrderStatusConsts.WORK_STATUS_DISTRIBUTE, OrderStatusConsts.WORK_STATUS_TAKE, OrderStatusConsts.WORK_STATUS_VISIT);

        //3.已完成

        //4.已取消 未完成+结果-弃单或失败
        //supportStatusList<已完成
        List<Integer> supportResultStatusList1 = Lists.newArrayList(OrderStatusConsts.WORK_RESULT_FAIL, OrderStatusConsts.WORK_RESULT_DISCARD);

        boolean resultStatusSupport = supportResultStatusList.contains(resultStatus);//进行中
        boolean statusSupport = supportStatusList.contains(status);
        if (resultStatusSupport && statusSupport) {
            vo.setStatusName("待派单");
        }

        boolean statusSupport1 = supportStatusList1.contains(status);
        if (resultStatusSupport && statusSupport1) {
            vo.setStatusName("服务中");
        }

        boolean statusSupport2 = status < OrderStatusConsts.WORK_STATUS_COMPLETE;
        boolean resultStatusSupport2 = supportResultStatusList1.contains(resultStatus);
        if (statusSupport2 && resultStatusSupport2) {
            vo.setStatusName("已取消");
        }
    }

}
